/**
 * Packet.java
 *
 * @author Philip Kwok
 */


package SSF.OS.OSPFv2;


import SSF.OS.*;
import SSF.Net.Util.IP_s;


// ========================= class SSF.OS.OSPFv2.Packet ====================== //
/**
  * This class implements a generic OSPF packet. The following from RFC 2178
  * are not included:
  * 1) OSPF Version number
  * 2) Packet length
  * 3) Checksum
  * 4) Authentication Type
  * 5) Authentication
  */
public class Packet extends ProtocolMessage {

  // ......................... constants ........................... //

  /** Indicates a hello packet. */
  public static final int HELLO     = 1;
  /** Indicates a database description packet. */
  public static final int DATABASE  = 2;
  /** Indicates a link state request packet. */
  public static final int LSREQUEST = 3;
  /** Indicates a link state update packet. */
  public static final int LSUPDATE  = 4;
  /** Indicates a link state acknowledgement packet. */
  public static final int LSACK     = 5;

  /** The standard length of an OSPF Packet header. */
  public static final int HEADER_LENGTH = 24;
  
  // ........................ member data .......................... //

  /** The type of this Packet. */
  public int type;
  
  /** The identification of the router which sent this packet, chosen as the
    * smallest of the IPaddress of all its interfaces. */
  public int router_ID;
  
  /** The NHI prefix of the sender. */
  public String nh;
  
  /** The NHI address prefix of the AS to which this packet belongs. */
  public String as_nh;

  /** The area to which this packet belongs. */
  public int areaNum;

  // -------------------------- constructor Packet ------------------------- //
  /**
   * Constructs a packet given type, router ID, area ID.
   */
  public Packet(int type, int router_ID, String nh, String as_nh, int areaNum) {
    this.type      = type;
    this.router_ID = router_ID;
    this.nh        = nh.substring(0);
    this.as_nh     = as_nh; // as_nh.substring(0);
    this.areaNum   = areaNum;
  }

  // ---------------------------- Packet.bytecount ---------------------------- //
  /** Return the total size (in bytes) of this OSPF Packet including the header. 
   *  All and only fields specified in RFC 2328 are included in calculation!!!
   */
  public int bytecount() {
    if (payload() == null) {
      return HEADER_LENGTH;
    } else {
      return (HEADER_LENGTH + payload().bytecount());
    }
  }

  // ------------------------ Packet.headerByteCount ------------------------- //
  /** Return the size (in bytes) of this Packet only, don't include the payload, if any. */
  public static int headerByteCount() {
    return HEADER_LENGTH;
  }
  
  // ---------------------------- Packet.toString -------------------------- //
  public String toString() {
    String s = "";
    s = type + " " + IP_s.IPtoString(router_ID) + " " + nh + " " + as_nh + " " +
        areaNum;
    return s;
  }


} // end class Packet


